package com.chebaohui.iqs.service.impl.deal.pingan.login;

import java.util.ArrayList;
import java.util.Map;

import org.apache.http.Consts;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.chebaohui.iqs.service.impl.deal.pingan.util.HttpClientUtil;

/**
 * 平安登录请求工具类
 * 
 * @author xielonghai
 *
 */
public class PAHttpUtil {
	private static final int socketTimeout = 1000 * 60;// 数据传输处理时间
	private static final int connectTimeout = 1000 * 60;// 连接时间

	/**
	 * get 请求
	 * 
	 * @param header
	 *            头部map
	 * @param url
	 *            访问url
	 * @return
	 */
	public static String GET(Map<String, String> header, String url) {
		CloseableHttpClient httpClient = null;
		HttpClientContext httpContext = null;
		HttpGet get = new HttpGet(url);
		CloseableHttpResponse response = null;
		try {
			httpClient = HttpClientUtil.createSSLClientDefault();
			httpContext = HttpClientContext.create();
			for (String key : header.keySet())
				get.setHeader(key, header.get(key));
			get.setConfig(getRequestConfig());
			response = httpClient.execute(get, httpContext);
			return EntityUtils.toString(response.getEntity());
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		} finally {
			try {
				if (null != response)
					response.getEntity().getContent().close();
				if (null != httpClient)
					httpClient.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	/**
	 * post json请求
	 * 
	 * @param header
	 *            头部map
	 * @param url
	 *            访问URL
	 * @param jsonParam
	 *            json参数
	 * @return
	 */
	public static String POST_Json(Map<String, String> header, String url, String jsonParam) {
		CloseableHttpClient httpClient = null;
		HttpClientContext httpContext = null;
		HttpPost httpPost = new HttpPost(url);
		CloseableHttpResponse response = null;
		try {
			httpClient = HttpClientUtil.createSSLClientDefault();
			httpContext = HttpClientContext.create();

			httpPost.setConfig(getRequestConfig());

			for (String key : header.keySet())
				httpPost.setHeader(key, header.get(key));

			StringEntity entity = new StringEntity(jsonParam, "UTF-8");
			entity.setContentEncoding("UTF-8");
			entity.setContentType("application/json");
			httpPost.setEntity(entity);

			response = httpClient.execute(httpPost, httpContext);
			return EntityUtils.toString(response.getEntity());
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		} finally {
			try {
				if (null != response)
					response.getEntity().getContent().close();
				if (null != httpClient)
					httpClient.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	/**
	 * post键值队请求
	 * 
	 * @param header
	 *            头部map
	 * @param url
	 *            访问url
	 * @param mapParam
	 *            参数map
	 * @return
	 */
	public static String POST(Map<String, String> header, String url, Map<String, String> mapParam) {
		CloseableHttpClient httpClient = null;
		HttpClientContext httpContext = null;
		HttpPost httpPost = new HttpPost(url);
		CloseableHttpResponse response = null;
		try {
			httpClient = HttpClientUtil.createSSLClientDefault();
			httpContext = HttpClientContext.create();

			httpPost.setEntity(new UrlEncodedFormEntity(getParam(mapParam), Consts.UTF_8));
			for (String key : header.keySet())
				httpPost.setHeader(key, header.get(key));

			httpPost.setConfig(getRequestConfig());

			response = httpClient.execute(httpPost, httpContext);

			return EntityUtils.toString(response.getEntity());
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		} finally {
			try {
				if (null != response)
					response.getEntity().getContent().close();
				if (null != httpClient)
					httpClient.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	/**
	 * 请求参数转化
	 * 
	 * @param mapParam
	 * @return
	 */
	private static ArrayList<BasicNameValuePair> getParam(Map<String, String> mapParam) {
		ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		for (String key : mapParam.keySet())
			params.add(new BasicNameValuePair(key, mapParam.get(key)));
		return params;
	}

	/**
	 * 请求设置
	 * 
	 * @return
	 */
	private static RequestConfig getRequestConfig() {
		return RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
	}

}
